<script setup lang="ts">
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";

let showPrivacy = ref(false);

let openPrivacyContract = () => {
  // @ts-ignore
  wx.openPrivacyContract({
    success: (res: any) => {
      console.log("openPrivacyContract success");
    },
    fail: (res: any) => {
      console.error("openPrivacyContract fail", res);
    },
  });
};

let resolvePrivacyAuthorization = ref<any>(null);

onLoad(() => {
  // @ts-ignore
  wx.onNeedPrivacyAuthorization((resolve: any, eventInfo: any) => {
    console.log("触发本次事件的接口是：" + eventInfo.referrer);
    // 需要用户同意隐私授权时
    // 弹出开发者自定义的隐私授权弹窗
    showPrivacy.value = true;
    resolvePrivacyAuthorization.value = resolve;
  });
});

let handleAgreePrivacyAuthorization = () => {
  resolvePrivacyAuthorization.value &&
    resolvePrivacyAuthorization.value({
      buttonId: "agree-btn",
      event: "agree",
    });
  showPrivacy.value = false;
};

let handleDisagree = () => {
  resolvePrivacyAuthorization.value &&
    resolvePrivacyAuthorization.value({
      buttonId: "disagree-btn",
      event: "disagree",
    });
  showPrivacy.value = false;
};
</script>

<template>
  <div class="PrivacyPopupWrap" v-if="showPrivacy">
    <div class="content">
      <div>
        <div class="title">莫某小程序</div>
      </div>
      <div>
        <div class="desc">
          在您使用【莫某小程序】服务之前，请仔细阅读
          <span class="book" @click="openPrivacyContract">
            《莫某小程序小程序隐私保护指引》
          </span>
          。如您同意
          <span class="book" @click="openPrivacyContract">
            《莫某小程序小程序隐私保护指引》
          </span>
          ，请点击“同意”开始使用【莫某小程序】。
        </div>
      </div>
      <div class="btn-wraps">
        <button id="disagree-btn" class="btn-disagree" @click="handleDisagree">
          拒绝
        </button>
        <button
          id="agree-btn"
          open-type="agreePrivacyAuthorization"
          class="btn-agree"
          @agreeprivacyauthorization="handleAgreePrivacyAuthorization"
        >
          同意
        </button>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.PrivacyPopupWrap {
  position: fixed;
  right: 0;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 999;
  background: rgba(0, 0, 0, 0.8);
  .content {
    position: absolute;
    width: 648rpx;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
    background: #fff;
    border-radius: 16rpx;

    box-sizing: border-box;
    padding: 0 35rpx;
    padding-top: 50rpx;
    padding-bottom: 32rpx;
  }
  .title {
    font-size: 34rpx;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: bold;
    color: #333030;
    text-align: center;
    margin-bottom: 20rpx;
  }
  .desc {
    font-size: 32rpx;
    font-weight: 400;
    color: #999999;
    margin-bottom: 44rpx;
  }
  .book {
    font-size: 32rpx;
    font-weight: 400;
    color: rgba(69, 165, 255, 1);
  }
  .btn-wraps {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
  .btn-disagree {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    width: 280rpx;
    height: 80rpx;
    background: #ffffff;
    border-radius: 16rpx;
    border: 1rpx solid #d7d7d7;
    font-weight: 400;
    font-size: 30rpx;
    color: #999999;
    margin: 0rpx;
  }
  .btn-agree {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    width: 280rpx;
    height: 80rpx;
    background: rgba(255, 58, 72, 1);
    border-radius: 16rpx;
    border: 1rpx solid rgba(255, 58, 72, 1);
    font-weight: 400;
    font-size: 30rpx;
    color: #fff;
    margin: 0rpx;
  }
}
</style>
